package com.htandroid.htandroid.webservice;

import android.util.Xml;

import com.htandroid.htandroid.entity.MrqgdSpList;
import com.htandroid.htandroid.entity.UffjqgdList;

import org.xmlpull.v1.XmlPullParser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class XMLMrqgdSplist {

    /**
     * 解析评论xml信息
     *
     * @param result
     * @return
     */
    public static ArrayList<MrqgdSpList> paraseCommentInfors(String result) {
        ArrayList<MrqgdSpList> list = new ArrayList<MrqgdSpList>();
        //字符转换成字节流
        InputStream inputStream = new ByteArrayInputStream(result.getBytes());
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();
            MrqgdSpList info = new MrqgdSpList();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
                        break;
                    case XmlPullParser.START_TAG:// 开始元素事件
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("MrqgdSpList")) {
                            info = new MrqgdSpList();
                        } else {
                            eventType = parser.next();
                            if (name.equalsIgnoreCase("cvouchid")) {
                                info.setCvouchid(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvouchnum")) {
                                info.setCvouchnum(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ddate")) {
                                info.setDdate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdepcode")) {
                                info.setCdepcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdepname")) {
                                info.setCdepname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dtime")) {
                                info.setDtime(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cmaker")) {
                                info.setCmaker(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ccheckerdate")) {
                                info.setCcheckerdate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cchecker")) {
                                info.setCchecker(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdefine1")) {
                                info.setCdefine1(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdefine2")) {
                                info.setCdefine2(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdefine3")) {
                                info.setCdefine3(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdefine4")) {
                                info.setCdefine4(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdepchecker")) {
                                info.setCdepchecker(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dckdate")) {
                                info.setDckdate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cbuyer")) {
                                info.setCbuyer(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cmemo")) {
                                info.setCmemo(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("al_inorder")) {
                                info.setAl_inorder(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("linename")) {
                                info.setLinename(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cbuyer")) {
                                info.setCbuyer(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("inorder")) {
                                info.setInorder(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("clinecode")) {
                                info.setClinecode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ccheckchar")) {
                                info.setCcheckchar(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("resign")) {
                                info.setResign(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cchar")) {
                                info.setCchar(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("stepall")) {
                                info.setStepall(string2null(parser.getText()));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:// 结束元素事件
                        if (parser.getName().equalsIgnoreCase("MrqgdSpList")) {
                            list.add(info);
                            info = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}  
